package com.mini.cn.goods.lottery;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class ProgrammeLottery extends LotteryGo {

  HashMap<String, List<Integer>> peopleArea;//每个用户的抽奖概率;
  private List<Integer> speed;
  private HashMap<Integer,String> lotteryPond;//最终的奖池


  @Override
  public void creatLottoeyBox() {
    int total=0;//奖池中的奖球总个数
    //清空抽奖球
    if (speed == null) {
      speed = new ArrayList<Integer>();
    }
    speed.clear();
    //清空奖池
    if(lotteryPond==null){
      lotteryPond=new HashMap<Integer, String>();
    }
    lotteryPond.clear();
    //生成奖池
    Iterator iterator = peopleArea.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry entry = (Map.Entry) iterator.next();
      List<Integer> numberlist = (List<Integer>) entry.getValue();
      int size=numberlist.size();
      //生成奖球
      for(int i=0;i<size;i++){
        speed.add(numberlist.get(i));
      }
      total+=size;
    }

    //生成最终的奖池 再次循环
    while (iterator.hasNext()){
      Map.Entry entry = (Map.Entry) iterator.next();
      List<Integer> numbers = (List<Integer>) entry.getValue();
      String  key=(String)entry.getKey();
      for(int i=0;i<numbers.size();i++){
        //把人随机放入奖池 得到随机数
        int pos= ThreadLocalRandom.current().nextInt(speed.size());
        int realPos=speed.get(pos);
        lotteryPond.put(realPos,key);
        speed.remove(pos);
      }
    }
  }

  @Override
  public String LotteryPrize() {
    int sum=lotteryPond.size();
    int rand = ThreadLocalRandom.current().nextInt(sum);
    System.out.print("sum=" + sum + "  rand=" + rand);
    return  String.valueOf(rand);
  }



  @Override
  public void clearLotteryBuffer() {
    peopleArea.clear();
    speed.clear();
    lotteryPond.clear();
    peopleArea=null;
    speed=null;
    lotteryPond=null;
  }
}
